package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityCardInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.payconfig.UpdatecardStateVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoCardInfo;
import com.ocom.payconfig.service.XfCardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class XfCardInfoServiceImpl extends ServiceImpl<IDaoCardInfo, EntityCardInfo> implements XfCardInfoService {


    @Autowired
    private IDaoCardInfo daoCardInfo;

    // 查询卡信息（getCardInfo）
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityCardInfo GetCardInfo(Long comId, String accountNo) {
        QueryWrapper<EntityCardInfo> query = Wrappers.query();
        query.eq("com_id", comId);
        query.eq("account_no", accountNo);
        query.in("card_state", 0, 1);
        query.orderByDesc("update_time");
        query.last("limit 1");
        return daoCardInfo.selectOne(query);
    }


    // 查询人员卡信息（getCardInfo）  有正常卡返回正常卡  无则返回最新的操作
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityCardInfo GetCardInfoByPid(Long comId, Long pId) {
        QueryWrapper<EntityCardInfo> query = Wrappers.query();
        query.eq("com_id", comId);
        query.eq("p_id", pId);
        query.eq("card_state", 1);
        EntityCardInfo entityCardInfo = daoCardInfo.selectOne(query);
        if (entityCardInfo != null) {
            return entityCardInfo;
        }
        QueryWrapper<EntityCardInfo> query1 = Wrappers.query();
        query1.eq("com_id", comId);
        query1.eq("p_id", pId);
        query1.eq("card_state", 0);
        query1.orderByDesc("card_state");
        query1.last("limit 1");
        return daoCardInfo.selectOne(query1);
    }


    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityCardInfo selectCount(Long comId, String cardNo) {
        QueryWrapper<EntityCardInfo> query = Wrappers.query();
        query.eq("com_id", comId);
        query.eq("card_no", cardNo);
        query.in("card_state", 0, 1);
        query.orderByDesc("update_time");
        query.last("limit 1");
        return daoCardInfo.selectOne(query);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Integer selectCardCount(Long comId, Long pId) {
        QueryWrapper<EntityCardInfo> query = Wrappers.query();
        query.eq("com_id", comId);
        query.eq("p_id", pId);
        query.in("card_state", 0, 1);
        return daoCardInfo.selectCount(query);
    }


    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public Integer selectAccountNo(Long comId, String accountNo) {
        QueryWrapper<EntityCardInfo> query = Wrappers.query();
        query.eq("com_id", comId);
        query.eq("account_no", accountNo);
        query.in("card_state", 0, 1);
        return daoCardInfo.selectCount(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void AddCardInfo(Long comId, EntityCardInfo entityCardInfo) {
        daoCardInfo.insert(entityCardInfo);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean UpdatecardState(Long comId, UpdatecardStateVo updatecardStateVo) {
        UpdateWrapper<EntityCardInfo> query = Wrappers.update();
        query.eq("com_id", comId);
        query.eq("p_id", updatecardStateVo.getPId());
        query.ne("card_state", 2);
        query.set("card_state", updatecardStateVo.getCardState());
        query.set("update_user", updatecardStateVo.getUser());
        return this.update(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateCardInfo(Long comId, Long pId, EntityCardInfo entityCardInfo) {
        UpdateWrapper<EntityCardInfo> query = Wrappers.update();
        query.eq("com_id", comId);
        query.eq("p_id", pId);
        query.in("card_state", 0, 1);
        daoCardInfo.update(entityCardInfo, query);
    }
}
